package com.topboya.linker.ui.activity;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.topboya.linker.R;
import com.topboya.linker.model.response.FileBytaskIdAndProductIdReponse;
import com.topboya.linker.model.response.HomeListResponse;
import com.topboya.linker.model.response.TaskFileResponse;
import com.topboya.linker.model.response.TaskProductResponse;
import com.topboya.linker.model.save.TaskinfoBean;
import com.topboya.linker.ui.adapter.SeeTaskFileAdapter;
import com.topboya.linker.ui.base.activity.BaseActivity;
import com.topboya.linker.ui.presenter.SeeTaskInfoAtPresenter;
import com.topboya.linker.ui.view.SeeTaskInfoAtView;

import java.util.ArrayList;
import java.util.List;

public class SeeTaskFileActivity extends BaseActivity<SeeTaskInfoAtView, SeeTaskInfoAtPresenter> implements SeeTaskInfoAtView {

    private static final String TAG = "SeeTaskFileActivity:";
    private TextView toolbar_title;
    private TextView tv_toolbar_right;
    private Toolbar toolbar;
    private RecyclerView rey_selectfile;
    private List<TaskinfoBean> taskinfoBeans;
    private SeeTaskFileAdapter seeTaskFileAdapter;
    private HomeListResponse.TaskList task;
    private boolean istaskFile =true;
    private String productID;



    @Override
    protected int getLayoutId() {

        return R.layout.activity_see_selectfile;
    }

    @Override
    protected void initView() {

        toolbar_title = findViewById(R.id.toolbar_title);
        rey_selectfile = findViewById(R.id.rey_selectfile);
        tv_toolbar_right = findViewById(R.id.tv_toolbar_right);
        toolbar = findViewById(R.id.toolbar);
        tv_toolbar_right.setVisibility(View.GONE);
        toolbar_title.setText("资料信息");
    }

    @Override
    protected void initData() {


        taskinfoBeans = new ArrayList<>();
        task = (HomeListResponse.TaskList) getIntent().getSerializableExtra("task");
        istaskFile = getIntent().getBooleanExtra("istaskFile",true);

        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this, 2);
        rey_selectfile.setLayoutManager(layoutManager);
//        rey_selectfile.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.HORIZONTAL, 2, 0xBFC0C2));
        seeTaskFileAdapter = new SeeTaskFileAdapter(this, (ArrayList<TaskinfoBean>) taskinfoBeans);
        rey_selectfile.setAdapter(seeTaskFileAdapter);
        if (istaskFile){

            mPresenter.getTaskFile(task.getId());

        }else {
            productID = getIntent().getStringExtra("productID");
            mPresenter.getFeekPruductFileBytaskIdAndProductId(task.getId(),productID);
        }

    }

    @Override
    protected void initListener() {

        toolbar.setNavigationOnClickListener(v -> {   back();});
        seeTaskFileAdapter.setOnItemClickListener((v, position) -> {


        });
    }

    @Override
    protected SeeTaskInfoAtPresenter createPresenter() {

        return new SeeTaskInfoAtPresenter(this);
    }

    //   重写返回键，怕用户点删除按钮了，直接返回
    @Override
    public void onBackPressed() {

        back();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // 是否触发按键为back键
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            onBackPressed();
            return true;
        } else { // 如果不是back键正常响应
            return super.onKeyDown(keyCode, event);
        }
    }

    private void back() {

         finish();

    }

    @Override
    public void getTaskProductList(List<TaskProductResponse.Data> taskProductResponse) {


    }

    @Override
    public void getTaskFile(List<TaskFileResponse.Data> taskfilelist) {

        if (null !=taskfilelist && taskfilelist.size() >0){
            taskinfoBeans.clear();
            for (TaskFileResponse.Data taskfile:taskfilelist){

                TaskinfoBean taskinfoBean = new TaskinfoBean();
                taskinfoBean.setFileName(taskfile.getFileName());
                taskinfoBean.setFileSuffix(taskfile.getFileSuffix());
                taskinfoBean.setId(taskfile.getId());
                taskinfoBean.setStoreMinPath(taskfile.getStoreMinPath());
                taskinfoBeans.add(taskinfoBean);
            }
            seeTaskFileAdapter.notifyDataSetChanged();
        }

    }

    @Override
    public void getFeekPruductFileBytaskIdAndProductId(List<FileBytaskIdAndProductIdReponse.FileList> filebytaskidlist) {

        if (null !=filebytaskidlist && filebytaskidlist.size() >0){
            taskinfoBeans.clear();
            for (FileBytaskIdAndProductIdReponse.FileList fileList:filebytaskidlist){

                TaskinfoBean taskinfoBean = new TaskinfoBean();
                taskinfoBean.setFileName(fileList.getFile_name());
                taskinfoBean.setFileSuffix(fileList.getFileSuffix());
                taskinfoBean.setId(fileList.getFileInforId());
                taskinfoBean.setStoreMinPath(fileList.getStoreMinPath());
                taskinfoBeans.add(taskinfoBean);
            }
            seeTaskFileAdapter.notifyDataSetChanged();
        }
    }
}
